package com.tiandang.controller;

import com.lly835.bestpay.model.RefundResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tiandang.core.error.BusinessException;
import tiandang.core.model.OrderInfo;
import tiandang.core.response.CommonReturnType;
import tiandang.core.service.OrderService;
import tiandang.core.service.RefundNotifyService;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/order")
public class OrderController {

  @Autowired
  private OrderService orderService;

  @Autowired
  private RefundNotifyService refundNotifyService;

  @GetMapping("/getList")
  public CommonReturnType getList(@RequestParam(value = "startTimestamp", required = false) Long startTimestamp,
                                  @RequestParam(value = "endTimestamp", required = false) Long endTimestamp,
                                  @RequestParam(value = "managerName", required = false) String managerName,
                                  @RequestParam(value = "productName", required = false) String productName,
                                  @RequestParam(value = "deliveryStatus", required = false) Integer deliveryStatus,
                                  @RequestParam(value = "orderId", required = false) String orderId,
                                  @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                  @RequestParam(value = "limit", defaultValue = "10") Integer limit) {

    return CommonReturnType.success(orderService.getList(startTimestamp, endTimestamp, managerName,
      productName, deliveryStatus, orderId, currentPage, limit));

  }

  @PostMapping("/createOrder")
  public CommonReturnType createOrder(@RequestBody OrderInfo order) throws BusinessException {
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("order", orderService.createOrder(order));
    return CommonReturnType.success(resultMap);
  }

  @GetMapping("/getDetail")
  public CommonReturnType getDetail(String id) {
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("order", orderService.getOrderById(id));
    return CommonReturnType.success(resultMap);
  }

  //退款
  @PostMapping("/refund")
  public CommonReturnType refund(@RequestParam(value = "id") String id) {
    orderService.updateOrderStatus(id, 3);

    OrderInfo order = orderService.getOrderById(id);

    //发起退款申请
    RefundResponse refundResponse = refundNotifyService.create(order);

    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("refundResponse", refundResponse);
    return CommonReturnType.success(resultMap);
  }

  //发货
  @PostMapping("/deliverProduct")
  public CommonReturnType deliverProduct(@RequestParam(value = "id") String id) {
    orderService.deliverProduct(id);
    return CommonReturnType.success();
  }

}
